ActionUtil={}

ActionUtil.createAction = function(plistname, dur)
  --print("getAction ", plistname)
  --local action = {}  
  local sfc=CCSpriteFrameCache:sharedSpriteFrameCache()
  local path=CCFileUtils:sharedFileUtils():fullPathFromRelativePath(plistname)
  print(path)
  local plistdata = CCDictionary:createWithContentsOfFile(path)
  local framedict = TypeCast(plistdata:objectForKey("frames"), "CCDictionary")
  local keys=framedict:allKeys()
 
  --sfc:addSpriteFramesWithFile(string.format("%s.plist", plistname))
  sfc:addSpriteFramesWithFile(plistname)
  --local frames=CCArray:create() 
  local animation=CCAnimation:create()
  print(keys:count())
  for i=0,keys:count()-1 do
    local name = TypeCast(keys:objectAtIndex(i), "CCString")
    print("key", name:getCString())
    local frame = sfc:spriteFrameByName(name:getCString())
    animation:addSpriteFrame(frame)
    --frames:addObject(frame)
  end
  animation:setDelayPerUnit(dur/keys:count())
  animation:setRestoreOriginalFrame(true)
  return CCAnimate:create(animation)
  
end

ActionUtil.createSeqAction = function(actions)
  local seq = CCArray:create()
  for i=1,#actions do
    print("ActionUtil.createSeqAction ",#actions,i, actions[i])
    seq:addObject(actions[i])
  end
  return CCSequence:create(seq)
end

--获取左下角坐标
ActionUtil.leftBottomPoint = function(layer) 
  local tx, ty = layer:getPosition()
  local size = layer:getContentSize()
  local anch = layer:getAnchorPoint()

  local x = tx - size.width*anch.x
  local y = ty - size.height*anch.y
  print("ActionUtil.leftBottomPoint ", x, y)
  return x,y
end

--获取node矩形区域
ActionUtil.getNodeRect = function(node)
  local tx, ty = node:getPosition()
  local size = node:getContentSize()
  local anch = node:getAnchorPoint()

  local x = tx - size.width*anch.x
  local y = ty - size.height*anch.y
  print("ActionUtil.leftBottomPoint ", x, y)
  return CCRectMake(x, y, size.width, size.height)
end

--将矩形缩小rate，并且保证位置不变
ActionUtil.innerRect = function(rect, rate)
  local width = rect.size.width*rate.x
  local height = rect.size.height*rate.y
  
  local x = rect.origin.x + rect.size.width*(1-rate.x)/2;
  local y = rect.origin.y + rect.size.height*(1-rate.y)/2; 

  return CCRectMake(x, y, width, height)
end
--ActionUtil.getAction("a")
